package com.sairobo.admin.modules.srblive.service.impl;

import com.sairobo.base.common.entity.Page;
import com.sairobo.base.common.entity.Query;
import com.sairobo.base.common.entity.R;
import com.sairobo.base.common.utils.Assert;
import com.sairobo.base.common.utils.CommonUtils;
import com.sairobo.base.common.utils.ShiroUtils;
import com.sairobo.admin.modules.srblive.entity.SrbliveClassroom;
import com.sairobo.admin.modules.srblive.entity.SrbliveUser;
import com.sairobo.admin.modules.srblive.service.SrbliveClassroomService;
import com.sairobo.admin.modules.srblive.service.SrbliveUserService;
import com.sairobo.admin.modules.srblive.manager.SrbliveClassroomManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

/**
 * 课题
 *
 * @author admin
 * @email sairobo
 * @url www.sairobo.com
 * @date 2018年1月11日 上午11:01:21
 */
@Service("srbliveClassroomService")
public class SrbliveClassroomServiceImpl implements SrbliveClassroomService {

	@Autowired
	private SrbliveClassroomManager srbliveClassroomManager;

    @Autowired
    SrbliveUserService srbliveUserService;

	@Override
	public Page<SrbliveClassroom> listSrbliveClassroom(Map<String, Object> params) {
		Query query = new Query(params);
		Page<SrbliveClassroom> page = new Page<>(query);
		srbliveClassroomManager.listSrbliveClassroom(page, query);
		return page;
	}

	@Override
	public R saveSrbliveClassroom(SrbliveClassroom role) {
        role.setClassroomid(UUID.randomUUID().toString().replace("-","").substring(23));

        //根据unionId获取openId
        SrbliveUser srbliveUser = srbliveUserService.getByunionId(ShiroUtils.getUserEntity().getUnionId());
        if(srbliveUser==null)
            throw new RuntimeException("请先关注《知心云》公众号再来操作！");
        else
            role.setOpenid(srbliveUser.getOpenid());


		int count = srbliveClassroomManager.saveSrbliveClassroom(role);
		return CommonUtils.msg(count);
	}

	@Override
	public R getSrbliveClassroomById(Long id) {
		SrbliveClassroom srbliveClassroom = srbliveClassroomManager.getSrbliveClassroomById(id);
		return CommonUtils.msg(srbliveClassroom);
	}

	@Override
	public R updateSrbliveClassroom(SrbliveClassroom srbliveClassroom) {
		int count = srbliveClassroomManager.updateSrbliveClassroom(srbliveClassroom);
		return CommonUtils.msg(count);
	}

	@Override
	public R batchRemove(Long[] id) {
//	    File file ;
//        SrbliveClassroom srbliveClassroom;
//        //删除附件
//        for (Long idd : id) {
//            srbliveClassroom =  this.srbliveClassroomManager.getSrbliveClassroomById(idd);
//            file = new File(srbliveClassroom.getPptpath());
//            file.deleteOnExit();
//        }
        int count = srbliveClassroomManager.batchRemove(id);
        return CommonUtils.msg(id, count);
	}

    @Override
    public R active(Long id, Integer active) {
        Assert.notNull(id);
        Assert.notNull(active);
        SrbliveClassroom classroom = srbliveClassroomManager.selectById(id);
        classroom.setActive(active);
        srbliveClassroomManager.updateSrbliveClassroom(classroom);
        return R.ok();
    }

    @Override
    public R end(Long id) {
        /*{name:'正常',value:1},{ name: '课程结束', value:0},*/
        Assert.notNull(id);
        SrbliveClassroom classroom = srbliveClassroomManager.selectById(id);
        classroom.setStatus(0);
        srbliveClassroomManager.updateSrbliveClassroom(classroom);
        return R.ok();
    }
}
